<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch } from 'vue'
import { TableData } from '@/api/table/types'
import { useValidator } from '@/hooks/web/useValidator'
import { FormSchema } from '@/types/form'

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<TableData>>,
    default: () => null
  },
})
const schema = reactive<FormSchema[]>([
  {
    field: 'productName',
    label: '商品名称',
    component: 'Input',
  },
  {
    field: 'productSate',
    label: '审核状态',
    component: 'Radio',
    componentProps: {
      options: [
        {
          label: '审核通过',
          value: 3
        },
        {
          label: '审核不通过',
          value: 4
        }
      ]
    }
  },
  {
    field: 'details',
    label: '反馈详情',
    component: 'Input',
  },
])

const { register, methods, elFormRef } = useForm({
  schema
})

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues } = methods
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>

<template>
  <Form :rules="rules" @register="register" />
</template>
